package com.hj.service.impl;

import com.hj.dao.CompanyMapper;
import com.hj.dao.ReSetMapper;
import com.hj.entity.*;
import com.hj.utils.MailHelper;
import com.hj.utils.MultipleDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;

@Component
public class ResetMission {

    private static Logger logger = LoggerFactory.getLogger(ResetMission.class);

    @Autowired
    private ReSetMapper resetmapper;

    @Autowired
    private CompanyMapper companymapper;

    @Autowired
    private MailHelper mailHelper;

    @Value("${cp.agency.need.closedown}")
    private String agencyNames;

    @Value("${cp.agency.need.ignore}")
    private String  agencyNameIgnores;

    @Scheduled(cron="0 0/4 * * * ?")
    public void resetJob() {

        String[] agency = agencyNames.split(",");
        String[] agencyIgnores = agencyNameIgnores.split(",");

        /*查询停机状态的400号码*/
        MultipleDataSource.setDataSourceKey("dataSource");
        List<Ss_accesscode> list_sac = resetmapper.search_ac();

        MultipleDataSource.setDataSourceKey("dataSource1");
        //查询到停机并且余额大于0的自有用户
        List<Company> cp = resetmapper.search_cp(list_sac);

        MultipleDataSource.setDataSourceKey("dataSource2");
        //查询到停机并且截止日大于当前日期的代理商用户
        List<Company> bcp = companymapper.search_cp(agency,agencyIgnores,list_sac);

        cp.addAll(bcp);

        if(cp.size()>0){
            //先修改company表状态
            companymapper.update_companyReset(cp);

            //查询上一次停机前的接入策略并复机
            Iterator<Company> companyIterator = cp.iterator();
            while(companyIterator.hasNext()){
                List<Ss_accesscode> accesscodeList = resetmapper.search_former(companyIterator.next());
                if(accesscodeList.size()>0){
                    for(Ss_accesscode ss_accesscode : accesscodeList){
                        MultipleDataSource.setDataSourceKey("dataSource");
                        resetmapper.update_former(ss_accesscode);

                        MultipleDataSource.setDataSourceKey("dataSource2");
                        resetmapper.delete_formerRecord(ss_accesscode);
                    }
                    companyIterator.remove();
                }
            }

            if(cp.size()>0){
                //查询绑定的是中继号并邮件通知复机
                List<MiddleNumber> middleNumberList = companymapper.search_middle_number(cp);
                if(middleNumberList.size()>0){
                    String middleNumberUser = "";
                    for(MiddleNumber middleNumber:middleNumberList){
                        Iterator<Company> iterator = cp.iterator();
                        //去除绑定中继号的400号码
                        while(iterator.hasNext()){
                            if(iterator.next().getCallee_number().equals(middleNumber.getCallee_number())){
                                iterator.remove();
                            }
                        }
                        middleNumberUser += middleNumber.getCallee_number()+" ";
                    }
                    logger.info("中继号复机通知： {}", middleNumberUser);
                    //邮件通知维护人员进行手动复机
                    mailHelper.mailto("", "停复机服务——中继号复机", middleNumberUser);
                }
            }

            MultipleDataSource.setDataSourceKey("dataSource");

            if(cp.size()>0){
                //查询需按路由设置的400号码并复机
                List<Cti_routeservice> c_rs=resetmapper.search_crs(cp);
                if(c_rs.size()>0){
                    String routeNumberUser = "";
                    for(Cti_routeservice cti_routeservice:c_rs){
                        Iterator<Company> iterator = cp.iterator();
                        //去除绑定路由的400号码
                        while(iterator.hasNext()){
                            if(iterator.next().getVccid().equals(cti_routeservice.getVccid())){
                                iterator.remove();
                            }
                        }
                        routeNumberUser += cti_routeservice.getVccid()+" ";
                        resetmapper.update_rs(cti_routeservice);
                    }
                    logger.info("路由服务复机： {}", routeNumberUser);
                }
            }

            if(cp.size()>0) {
                //查询需按ivr设置的400号码并复机
                List<Cti_ivrservice> c_is = resetmapper.search_is(cp);
                if (c_is.size() > 0) {
                    String ivrNumberUser = "";
                    for (Cti_ivrservice cti_ivrservice : c_is) {
                        //去除绑定ivr的400号码
                        Iterator<Company> iterator = cp.iterator();
                        //去除绑定路由的400号码
                        while(iterator.hasNext()) {
                            if (iterator.next().getVccid().equals(cti_ivrservice.getVccId())) {
                                iterator.remove();
                            }
                        }
                        ivrNumberUser += cti_ivrservice.getVccId()+" ";
                        resetmapper.update_is(cti_ivrservice);
                    }
                    logger.info("IVR服务复机： {}", ivrNumberUser);
                }
            }

            if(cp.size()>0) {
                //查询需按人工设置的400号码并复机
                List<Cti_humanservice> c_hs = resetmapper.search_hs(cp);
                if (c_hs.size() > 0) {
                    String humanNumberUser = "";
                    for (Cti_humanservice cti_humanservice : c_hs) {
                        humanNumberUser += cti_humanservice.getVccId()+" ";
                        resetmapper.update_hs(cti_humanservice);
                    }
                    logger.info("人工服务复机： {}", humanNumberUser);
                }
            }
        }
    }
}
